package com.linjj.seckill.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linjj.seckill.access.AccessLimit;
import com.linjj.seckill.domain.User;
import com.linjj.seckill.service.IOrderService;
import com.linjj.seckill.vo.CodeMsg;
import com.linjj.seckill.vo.OrderDetailVo;
import com.linjj.seckill.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 订单控制器
 * */
@RequestMapping("/order")
@Controller
public class OrderController {
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单详情
     * */
    @AccessLimit(seconds = 5,maxCount = 5,needLogin = true)
    @GetMapping("/detail/{orderId}")
    @ResponseBody
    public Result detail(User user,@PathVariable("orderId") long orderId){
        OrderDetailVo orderDetailVo = orderService.getOrderDetailVoByOrderId(orderId);
        if(orderDetailVo.getOrder().getUserId().equals(user.getUserId())){
            return Result.success(orderDetailVo);
        }else{
            return Result.error(CodeMsg.REQUEST_ILLEGAL);
        }
    }
    /**
     * 查询用户订单
     * */
    @AccessLimit(seconds = 5,maxCount = 5,needLogin = true)
    @GetMapping("/list")
    @ResponseBody
    public Result list(User user,int page){
        PageHelper.startPage(page,5);
        List<OrderDetailVo> orders = orderService.getUserOrders(user);
        return Result.success(new PageInfo<>(orders));
    }
}
